// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-04-26 15:54:41
// 生成路径: internal/app/business/logic/ticket_remain.go
// 生成人：周志雄
// desc:节目票剩余
// company:云南奇讯科技有限公司
// ==========================================================================


package logic


import (
    "context"
    "github.com/gogf/gf/v2/frame/g"    
    "github.com/gogf/gf/v2/util/gconv"    
    "github.com/tiger1103/gfast/v3/api/v1/business"
    "github.com/tiger1103/gfast/v3/internal/app/business/dao"
    "github.com/tiger1103/gfast/v3/internal/app/business/model"
    "github.com/tiger1103/gfast/v3/internal/app/business/model/do"
    "github.com/tiger1103/gfast/v3/internal/app/business/service"    
    "github.com/tiger1103/gfast/v3/internal/app/system/consts"    
    "github.com/tiger1103/gfast/v3/library/liberr"
)


func init() {
	service.RegisterTicketRemain(New())
}


func New() *sTicketRemain {
	return &sTicketRemain{}
}


type sTicketRemain struct{}


func (s *sTicketRemain)List(ctx context.Context, req *business.TicketRemainSearchReq) (listRes *business.TicketRemainSearchRes, err error){
    listRes = new(business.TicketRemainSearchRes)
    err = g.Try(ctx, func(ctx context.Context) {
        m := dao.TicketRemain.Ctx(ctx).WithAll()          
        if req.Id != "" {
            m = m.Where(dao.TicketRemain.Columns().Id+" = ?", req.Id)
        }          
        if req.PerformanceId != "" {
            m = m.Where(dao.TicketRemain.Columns().PerformanceId+" = ?", gconv.Int(req.PerformanceId))
        }          
        if req.TicketType != "" {
            m = m.Where(dao.TicketRemain.Columns().TicketType+" = ?", req.TicketType)
        }          
        if req.RemainNum != "" {
            m = m.Where(dao.TicketRemain.Columns().RemainNum+" = ?", gconv.Int(req.RemainNum))
        }            
        if len(req.DateRange) != 0 {
            m = m.Where(dao.TicketRemain.Columns().CreatedAt+" >=? AND "+dao.TicketRemain.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
        }        
        listRes.Total, err = m.Count()
        liberr.ErrIsNil(ctx, err, "获取总行数失败")
        if req.PageNum == 0 {
            req.PageNum = 1
        }
        listRes.CurrentPage = req.PageNum
        if req.PageSize == 0 {
            req.PageSize = consts.PageSize
        }
        order:= "id asc"
        if req.OrderBy!=""{
            order = req.OrderBy
        }
        var res []*model.TicketRemainInfoRes
        err = m.Fields(business.TicketRemainSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)        
        liberr.ErrIsNil(ctx, err, "获取数据失败")
        listRes.List = make([]*model.TicketRemainListRes,len(res))
        for k,v:=range res{            
            listRes.List[k] = &model.TicketRemainListRes{                    
                Id : v.Id,                    
                PerformanceId : v.PerformanceId,                    
                TicketType : v.TicketType,                    
                RemainNum : v.RemainNum,                    
                CreatedAt : v.CreatedAt,                
            }
        }
    })
    return
}


func (s *sTicketRemain)GetById(ctx context.Context, id int) (res *model.TicketRemainInfoRes,err error){
    err =g.Try(ctx, func(ctx context.Context){
        err = dao.TicketRemain.Ctx(ctx).WithAll().Where(dao.TicketRemain.Columns().Id,  id).Scan(&res)
        liberr.ErrIsNil(ctx,err,"获取信息失败")
    })
    return
}


func (s *sTicketRemain)Add(ctx context.Context, req *business.TicketRemainAddReq) (err error){
    err = g.Try(ctx, func(ctx context.Context) {        
        _, err = dao.TicketRemain.Ctx(ctx).Insert(do.TicketRemain{            
            Id:req.Id,            
            PerformanceId:req.PerformanceId,            
            TicketType:req.TicketType,            
            RemainNum:req.RemainNum,            
        })
        liberr.ErrIsNil(ctx, err, "添加失败")
    })
    return
}


func (s *sTicketRemain)Edit(ctx context.Context, req *business.TicketRemainEditReq) (err error){
    err = g.Try(ctx, func(ctx context.Context) {        
        _, err = dao.TicketRemain.Ctx(ctx).WherePri(req.Id).Update(do.TicketRemain{            
            PerformanceId:req.PerformanceId,            
            TicketType:req.TicketType,            
            RemainNum:req.RemainNum,            
        })
        liberr.ErrIsNil(ctx, err, "修改失败")
    })
    return
}


func (s *sTicketRemain)Delete(ctx context.Context, ids []int) (err error){
    err = g.Try(ctx,func(ctx context.Context){        
        _, err = dao.TicketRemain.Ctx(ctx).Delete(dao.TicketRemain.Columns().Id+" in (?)", ids)
        liberr.ErrIsNil(ctx,err,"删除失败")
    })
    return
}


